package com.example.demoface.service.impl;


import com.alibaba.fastjson.JSON;
import com.example.demoface.domian.ResponseResult;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@Service
public class BaiduFaceService {

    public  ResponseResult add(String img,String userId) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("image", img);
            map.put("group_id", "test");
            map.put("user_id", userId);
            map.put("user_info", "aaa");
            map.put("liveness_control", "NONE");
            map.put("image_type", "BASE64");
            map.put("quality_control", "LOW");

            String jsonString = JSON.toJSONString(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.417906f29de0d4e671f925ba3da44cf2.2592000.1745393657.282335-14420661";
            OkHttpClient client = new OkHttpClient();

            // 创建请求体
            RequestBody requestBody = RequestBody.create(
                    jsonString,
                    okhttp3.MediaType.get("application/json; charset=utf-8")
            );

            // 创建请求
            Request request = new Request.Builder()
                    .url(url+"?access_token="+accessToken)
                    .post(requestBody)
                    .build();
            Response execute = client.newCall(request).execute();
            String s = execute.body().string();
            ResponseResult responseResult = JSON.parseObject(s, ResponseResult.class);
            return responseResult;


        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

    public  ResponseResult search(String img,String userId) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("image", img);
            map.put("group_id", "test");
            map.put("user_id", userId);
            map.put("user_info", "aaa");
            map.put("liveness_control", "NONE");
            map.put("image_type", "BASE64");
            map.put("quality_control", "LOW");

            String jsonString = JSON.toJSONString(map);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.417906f29de0d4e671f925ba3da44cf2.2592000.1745393657.282335-14420661";
            OkHttpClient client = new OkHttpClient();

            // 创建请求体
            RequestBody requestBody = RequestBody.create(
                    jsonString,
                    okhttp3.MediaType.get("application/json; charset=utf-8")
            );

            // 创建请求
            Request request = new Request.Builder()
                    .url(url+"?access_token="+accessToken)
                    .post(requestBody)
                    .build();
            Response execute = client.newCall(request).execute();
            String s = execute.body().string();
            ResponseResult responseResult = JSON.parseObject(s, ResponseResult.class);
            return responseResult;


        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

}
